package com.resou.reader.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.resou.reader.R;
import com.resou.reader.api.entry.ChannelBean;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import java.nio.channels.Channel;
import java.util.List;

public class MagincIndicatorAdapter extends CommonNavigatorAdapter {
    List<String> list;
    List<ChannelBean> list2;
    ViewPager mViewPager;
    boolean isChannel = false;
    int normalColor = 0;
    int selectedColor = 0;
    int bottomLineColor = 0;

    public MagincIndicatorAdapter(List<String> list, ViewPager mViewPager) {
        this.list = list;
        this.mViewPager = mViewPager;
        normalColor = 0;
        selectedColor = 0;
    }

    public MagincIndicatorAdapter(List<ChannelBean> list, ViewPager mViewPager, boolean isChannel) {
        this.list2 = list;
        this.mViewPager = mViewPager;
        this.isChannel = isChannel;
        normalColor = 0;
        selectedColor = 0;
    }

    @Override
    public int getCount() {
        if (isChannel) {
            return list2 == null ? 0 : list2.size();
        } else {
            return list == null ? 0 : list.size();
        }
    }


    @Override
    public IPagerTitleView getTitleView(Context context, int i) {
        CustomTitleView customTitleView = new CustomTitleView(context);
        customTitleView.setNormalColor(normalColor == 0 ?
                context.getResources().getColor(R.color.color_ff333333) : normalColor);
        customTitleView.setSelectedColor(selectedColor == 0 ?
                context.getResources().getColor(R.color.theme_color) : selectedColor);

        if (isChannel) {
            customTitleView.setText(list2.get(i).getChannelDesc());
        } else {
            customTitleView.setText(list.get(i));
        }
        customTitleView.setTextSize(16);
        customTitleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(i);
            }
        });
        return customTitleView;
    }

    @Override
    public IPagerIndicator getIndicator(Context context) {
        LinePagerIndicator indicator = new LinePagerIndicator(context);
        indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
        indicator.setColors(bottomLineColor == 0 ?
                context.getResources().getColor(R.color.theme_color) : bottomLineColor);
        indicator.setLineHeight(6);
        return indicator;
    }

    @Override
    public float getTitleWeight(Context context, int index) {
        return super.getTitleWeight(context, 1);
    }

    /**
     * 设置未选中的颜色
     *
     * @param color
     * @return
     */
    public MagincIndicatorAdapter setNormalColor(int color) {
        this.normalColor = color;
        return this;
    }

    /**
     * 设置选中颜色
     *
     * @param color
     * @return
     */
    public MagincIndicatorAdapter setSelectColor(int color) {
        this.selectedColor = color;
        return this;
    }

    /**
     * 设置底部线条颜色
     *
     * @param color
     * @return
     */
    public MagincIndicatorAdapter setBottomLineColor(int color) {
        this.bottomLineColor = color;
        return this;
    }

}
